const DB = require('../core/DB')
const {
    Op
} = require('sequelize')

const Mail = require('../core/Mail')

class SystemController {
    // 获取用户ip
    async getIP(ctx) {
        global.Response.success(ctx, undefined, {
            ip: global.utils.getClientIP(ctx)
        })
    }

    // 获取用户ip记录
    async getIPLog(ctx) {
        // 参数
        let {
            page = 1, size = 10 } = ctx.query

        const {
            count,
            rows
        } = await DB.ip.findAndCountAll()
        const data = {
            rows,
            meta: {
                page: page, // 当前页数
                size, // 每页条目
                count: count, // 条目总数
                total: Math.ceil(count / size), // 总页数
            }
        }
        global.Response.success(ctx, undefined, data)
    }

    // 记录访问人员
    async recordIP(ctx) {
        try {
            let {
                ip = global.utils.getClientIP(ctx),
                remark
            } = ctx.request.body

            if (ip == '127.0.0.1' || ip == '::1') {
                global.Response.success(ctx, undefined, true)
                return
            }

            const result = await DB.ip.create({
                ip,
                remark,
                createdAt: global.Moment().format('YYYY-MM-DD HH:mm:ss'),
                updatedAt: global.Moment().format('YYYY-MM-DD HH:mm:ss')
            })

            global.Response.success(ctx, undefined, result)
        } catch (error) {
            global.Response.error(error)
        }
    }


    async sendMailCode(ctx) {
        const {
            email
        } = ctx.request.body

        let code = Mail.sendCode(email)

        global.mails[email] = code

        // 五分钟内有效
        setTimeout(() => {
            delete global.mails[email]
        }, 5 * 60000)

        global.Response.success(ctx, undefined, {
            email
        })
    }

    async verifyMailCode(ctx) {
        const {
            email,
            code,
            login = false // 可选值 邮箱登录返回用户信息
        } = ctx.request.body

        if (code == global.mails[email]) {
            let data = {
                email
            }
            if (login) {
                // 查找该邮箱用户信息
                let user = await DB.user.findOne({
                    where: {
                        email
                    }
                })

                let lastLoginAt = await user.get('updatedAt')
                // 更新登录时间
                user.updatedAt = global.Moment().format('YYYY-MM-DD HH:mm:ss')
                await user.save()

                data = {
                    user: {
                        adminId: user.id,
                        name: user.name,
                        email: user.email,
                        admin: user.admin,
                        lastLoginAt
                    },
                    token: uPutToken(user.id)
                }

            }
            global.Response.success(ctx, '验证通过', data)
        } else {
            global.Response.fail(ctx, '验证失败')
        }

    }

    async sendMail(ctx) {
        const {
            email,
            content
        } = ctx.request.body
        Mail.sendEmail(email, content || '消息已发送，请等待博主回复~')
        global.Response.success(ctx, undefined, {
            email,
            content
        })
    }
}

module.exports = new SystemController